package com.hzncc.scaffold.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.hzncc.scaffold.entity.Bo.AskDetailBo;
import com.hzncc.scaffold.entity.Bo.CourseAskBo;
import com.hzncc.scaffold.entity.Bo.MyAskBo;
import com.hzncc.scaffold.entity.Other.Ask;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzncc.scaffold.entity.Bo.AskBo;
import com.hzncc.scaffold.entity.Vo.AskReplyVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 提问问题表 Mapper 接口
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Component
public interface AskMapper extends BaseMapper<Ask> {
    @Select("SELECT\n" +
            "\t`ask`.`id`,\n" +
            "\t`ask`.`title`,\n" +
            "\t`ask`.`createTime`,\n" +
            "\t`ask`.`content`,\n" +
            "\t`ask`.`status`,\n" +
            "\t`ask`.`pubTime`,\n" +
            "\t`creator`.`id` AS `creatorId`,\n" +
            "\t`creator`.`name` AS `creatorName`,\n" +
            "\t`creator`.`role` AS `creatorRole`,\n" +
            "\t`creator`.`account` AS `creatorAccount`,\n" +
            "\t`creator`.`telephone` AS `creatorTelephone`,\n" +
            "\t`creator`.`realname` AS `creatorRealname`,\n" +
            "\t`creator`.`instituteName` AS `creatorInstituteName`,\n" +
            "\t`publisher`.`id` AS `publisherId`,\n" +
            "\t`publisher`.`name` AS `publisherName`,\n" +
            "\t`course`.`id` AS `courseId`,\n" +
            "\t`course`.`title` AS `courseTitle` \n" +
            "FROM\n" +
            "\t`ask` AS `ask`\n" +
            "\tLEFT OUTER JOIN `user` AS `creator` ON `ask`.`userId` = `creator`.`id`\n" +
            "\tLEFT OUTER JOIN `user` AS `publisher` ON `ask`.`publisherId` = `publisher`.`id`\n" +
            "\tLEFT OUTER JOIN `course` AS `course` ON `ask`.`courseId` = `course`.`id` \n ${ew.customSqlSegment}" +
            "ORDER BY\n" +
            "\t`ask`.`createTime` DESC,\n" +
            "\t`ask`.`id` DESC")
    public List<AskBo> list(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);



    @Select("SELECT\n" +
            "\t`ask`.`id`,\n" +
            "\t`ask`.`status`,\n" +
            "\t`ask`.`tag`,\n" +
            "\t`ask`.`createTime`,\n" +
            "\t`ask`.`title`,\n" +
            "\t`ask`.`content`,\n" +
            "\t`course`.`id` AS `courseId`,\n" +
            "\t`course`.`title` AS `courseTitle` \n" +
            "FROM\n" +
            "\t`ask` AS `ask`\n" +
            "\tLEFT OUTER JOIN `course` AS `course` ON `ask`.`courseId` = `course`.`id` ${ew.customSqlSegment}")
    public List<MyAskBo> myAsk(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\t`ask`.`id`,\n" +
            "\t`ask`.`title`,\n" +
            "\t`ask`.`tag`,\n" +
            "\t`ask`.`pubTime`,\n" +
            "\t`ask`.`scannedNum`,\n" +
            "\t`ask`.`replyNum`,\n" +
            "\t`creator`.`id` AS `creatorId`,\n" +
            "\t`creator`.`name` AS `creatorName`,\n" +
            "\t`creator`.`icon` AS `creatorIcon` \n" +
            "FROM\n" +
            "\t`ask` AS `ask`\n" +
            "\tLEFT OUTER JOIN `user` AS `creator` ON `ask`.`userId` = `creator`.`id` ${ew.customSqlSegment}")
    public List<CourseAskBo> courseAsk(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\t`ask`.`id`,\n" +
            "\t`ask`.`title`,\n" +
            "\t`ask`.`content`,\n" +
            "\t`ask`.`tag`,\n" +
            "\t`ask`.`pubTime`,\n" +
            "\t`ask`.`scannedNum`,\n" +
            "\t`ask`.`lastReplyTime`,\n" +
            "\t`creator`.`id` AS `creatorId`,\n" +
            "\t`creator`.`name` AS `creatorName`,\n" +
            "\t`creator`.`icon` AS `creatorIcon`\n" +
            "FROM\n" +
            "\t`ask` AS `ask`\n" +
            "\tLEFT OUTER JOIN `user` AS `creator` ON `ask`.`userId` = `creator`.`id`\n ${ew.customSqlSegment}")
    public AskDetailBo askDetail(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\treply.id,\n" +
            "\treply.content,\n" +
            "\treply.pubTime,\n" +
            "\treply.userId\n" +
            "FROM\n" +
            "\t`reply`\n" +
            "LEFT JOIN `user` ON user.id = reply.userId ${ew.customSqlSegment}")
    public List<AskReplyVo>
    replyList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);
}
